package com.nenglai.common.crud;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nenglai.common.response.ErrorCode;
import com.nenglai.common.response.ResponseException;
import com.nenglai.common.util.SpringUtil;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表单体
 *
 * @author kongdehui
 * @date 2021/04/22 22:36
 **/
@Data
public class EditorBody {
    /**
     * 数据
     */
    private List<JSONObject> editor = new ArrayList<>();

    /**
     * 实体类型字典
     */
    @JsonIgnore
    private Map<String, Class<?>> parallel;

    @JsonIgnore
    private static UpdateUtil updateUtil;
    @JsonIgnore
    private static EditorUtil editorUtil;

    /**
     * 统计有效条件数量
     */
    @JsonIgnore
    private int conditionTotal = 0;

    /**
     * 查询条件
     */
    private List<JSONObject> conditions;

    static {
        updateUtil = SpringUtil.getBean(UpdateUtil.class);
        editorUtil = SpringUtil.getBean(EditorUtil.class);
    }

    /**
     * @param updateWrapper
     * @param clazz
     * @param safe
     * @return com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<T>
     * @author kongdehui
     * @date 2021/04/22 20:04
     */
    public <T> UpdateWrapper<T> query(UpdateWrapper<T> updateWrapper, Class<?> clazz, boolean safe) {
        this.init(clazz);
        this.where(updateWrapper);
        this.load(updateWrapper);
        if (safe && this.conditionTotal == 0) {
            throw new ResponseException(ErrorCode.FORBID_NO_CONDITION_UPDATE);
        }
        return updateWrapper;
    }

    /**
     * 加载值
     *
     * @param query
     * @return void
     * @author kongdehui
     * @date 2021/04/30 19:37
     */
    private void load(UpdateWrapper<?> query) {
        if (editor.size() == 0) {
            throw new ResponseException(ErrorCode.EDITOR_EMPTY_CODE);
        }
        for (JSONObject next : editor) {
            editorUtil.set(query, next, parallel);
        }
    }

    /**
     * 初始化（实体类型字典）
     *
     * @param clazz
     * @return void
     * @author kongdehui
     * @date 2021/04/21 18:45
     */
    private void init(Class<?> clazz) {
        Map<String, Class<?>> parallel = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        Field field;
        for (Field value : fields) {
            try {
                field = value;
                field.setAccessible(true); // 开启访问权限
                parallel.put(field.getName(), field.getType());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.parallel = parallel;
    }

    /**
     * 条件查询
     *
     * @param query
     * @return void
     * @author kongdehui
     * @date 2021/04/22 20:04
     */
    public void where(UpdateWrapper<?> query) {
        if (conditions != null) {
            for (JSONObject next : conditions) {
                if (updateUtil.condition(query, next, parallel))
                    this.conditionTotal++;
            }
        }
    }
}
